package com.asiainfo.lop;

import android.content.Intent;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.asiainfo.lop.net.VolleyInterface;
import com.asiainfo.lop.net.VolleyRequest;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * 人群列表Activity
 * Created by wucm(wucm@asiainfo.com) on 2016/5/26 13:36.
 */
@EActivity(R.layout.crowd_detail)
public class CrowdDetailActivity extends BaseActivity {
    @ViewById(R.id.top_bar_tv)
    TextView topbarTv;
    @ViewById(R.id.crowd_detail_groupid)
    TextView groupIdTv;
    @ViewById(R.id.crowd_detail_grouptype)
    TextView groupTypeTv;
    @ViewById(R.id.crowd_detail_warntype)
    TextView wanrTypeTv;
    @ViewById(R.id.crowd_detail_groupcome)
    TextView groupComeTv;
    @ViewById(R.id.crowd_detail_groupnum)
    TextView groupNumTv;
    @ViewById(R.id.crowd_detail_grouptime)
    TextView groupTimeTv;

    @Click({R.id.btn_back_iv, R.id.btn_back_tv})
    void back() {
        this.finish();
    }

    @AfterViews
    void setTopBarTv() {
        topbarTv.setText("人群信息");
    }

    /**
     * 处理信息反馈
     */
    @Click(R.id.message_deal_feedback)
    void feedback(){
        Intent indent = new Intent(this, MessageFeedbackActivity_.class);
        startActivity(indent);
    }

    @AfterViews
    void init() {
        refreshData();
    }

    void refreshData() {
        String url = getString(R.string.rest_url_rooturl) + getString(R.string.rest_url_crowd_groupdetail);

        url = url.replace("{groupId}", "1");

        VolleyInterface volleyInterface = new VolleyInterface(this) {
            @Override
            public void success(JSONObject result) {
                flag = result.getString("flag");
                reason = result.getString("reason");

                if (!RestResultInfo.RESULT_SUCCESS_FLAG.equals(flag)) {
                    Toast.makeText(CrowdDetailActivity.this, reason, Toast.LENGTH_LONG).show();
                    return;
                }

                loadData(result.getJSONObject("data"));
            }

            @Override
            public void error(VolleyError volleyError) {
            }
        };
        VolleyRequest.get(this, url, "CrowdDetailActivity", volleyInterface);
    }

    /**
     * 加载数据
     *
     * @param data
     */
    void loadData(JSONObject data) {
        String groupId = data.getString("groupId");
        String groupName = data.getString("groupName");
        String groupTypeId = data.getString("groupTypeId");
        String groupTypeName = data.getString("groupTypeName");
        String warnTypeId = data.getString("warnTypeId");
        String warnTypeName = data.getString("warnTypeName");
        String groupSource = data.getString("groupSource");
        String groupCnt = data.getString("groupCnt");
        String groupCreateTime = data.getString("groupCreateTime");

        groupIdTv.setText(groupId);
        groupTypeTv.setText(groupTypeName);
        wanrTypeTv.setText(warnTypeName);
        groupComeTv.setText(groupSource);
        groupNumTv.setText(groupCnt);
        groupTimeTv.setText(groupCreateTime);
    }
}
